package com.org.cloud.lg.service;

import com.org.cloud.lg.api.MBaseServiceImpl;
import com.org.cloud.lg.api.MenuService;
import com.org.cloud.lg.api.RoleMenuService;
import com.org.cloud.lg.dto.MenuDto;
import com.org.cloud.lg.entity.Menu;
import com.org.cloud.lg.utils.Tree;
import com.org.cloud.lg.mapper.MenuMapper;
import com.org.cloud.lg.utils.BuildTree;
import com.org.cloud.lg.utils.Checker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-03-21
 */
@Service("menuService")
public class MenuServiceImpl extends MBaseServiceImpl<MenuDto,Menu,MenuMapper> implements MenuService {
	
	@Autowired
	RoleMenuService roleMenuService;
	

	public boolean save(MenuDto menuDto) {
		return super.insert(menuDto);
	}

	@Override
	public boolean update(MenuDto menuDto) {
		return super.updateByPk(menuDto);
	}

	@Override
	public Tree<MenuDto> selectTreeList() {
		Map<String, Object> map=new HashMap<>(16);
		List<MenuDto> menus=listDtoByMap(map);
		if(menus!=null&&!menus.isEmpty()){
			List<Tree<MenuDto>> trees = new ArrayList<Tree<MenuDto>>();
			for (MenuDto sysMenuDO : menus) {
				Tree<MenuDto> tree = new Tree<MenuDto>();
				tree.setId(sysMenuDO.getId());
				tree.setKey(sysMenuDO.getId());
				tree.setParentId(sysMenuDO.getParentId());
				tree.setName(sysMenuDO.getName());
				tree.setSpread(true);
				Map<String, Object> attributes = new HashMap<>(16);
				attributes.put("url", sysMenuDO.getUrl());
				attributes.put("icon", sysMenuDO.getIcon());
				attributes.put("perms", sysMenuDO.getPerms());
				attributes.put("type", sysMenuDO.getType());
				attributes.put("orderNum", sysMenuDO.getOrderNum());
				tree.setAttributes(attributes);
				trees.add(tree);
			}
			Tree<MenuDto> t = BuildTree.build(trees);
			return t;
		}else{
			return null;
		}
	}

	@Override
	public List<MenuDto> selectMenuTreeByUid(String userId) {
		return MbMapper.selectMenuTreeByUid(userId);
	}

	@Override
	public Set<String> selectPermsByUid(String userId) {
		    List<String> perms= MbMapper.selectPermsByUid(userId);
			Set<String> permsSet = new HashSet<>();
			if(Checker.BeNotEmpty(perms)){
				for (String perm : perms) {
					if(Checker.BeNotBlank(perm)) {
						permsSet.addAll(Arrays.asList(perm.trim().split(",")));
					}
				}
			}
			return permsSet;
	}

	@Override
	public Set<String> selectPermsByUidAndUrl(String userId,String url) {
		List<String> perms= MbMapper.selectPermsByUidAndUrl(userId,url);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if(Checker.BeNotBlank(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}

	@Override
	public List<MenuDto> selectMenuUid(String userId) {
		return MbMapper.selectMenuUid(userId);
	}

	@Override
	public MenuDto info(String id) {
		MenuDto chileMenuDto=getByPk(id);
		MenuDto parentMenuDto=getByPk(chileMenuDto.getParentId());
		if(Checker.BeNotNull(parentMenuDto)){
			chileMenuDto.setParentName(parentMenuDto.getName());
		}else{
			chileMenuDto.setParentName("根目录");
		}
		return chileMenuDto;
	}

	@Override
	public boolean delete(String id) {
		MenuDto menu=getByPk(id);
		if(menu.getType()==0||menu.getType()==1){//目录或者菜单需要递归删除
			List<String> ids=new ArrayList<>();
			ids.add(id);
			selectChilden(id,ids);
			boolean f= super.removeByIds(ids);
			if (f){//删除菜单或者目录成功时将删除，角色-菜单关系表
				for(String menuId:ids){
					roleMenuService.deleteByMenuId(menuId);
				}
				return true;
			}
		}
		else if(menu.getType()==2)//按钮
		{
			return super.removeById(id);
		}
		return false;
	}

	private void selectChilden(String id,List<String> list){
		Map<String, Object> param=new HashMap<>(16);
		param.put("parent_id", id);
		List<MenuDto> menus=listDtoByMap(param);
		if(null!=menus&&!menus.isEmpty()){
			for(MenuDto menu:menus){
				list.add(menu.getId());
				selectChilden(menu.getId(),list);
			}
		}
	}

}
